package cn.zhuama.org.controller;

import cn.zhuama.basic.attention.PreAuthorize;
import cn.zhuama.basic.util.AjaxResult;
import cn.zhuama.basic.util.PageList;
import cn.zhuama.org.domain.Department;
import cn.zhuama.org.query.DepartmentQuery;
import cn.zhuama.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    @PreAuthorize(value = "department/findOne",name = "查询一个部门根据ID")
    public Department finOne( @ApiParam(name="编号",required = true) @PathVariable("id") Long id){
        return departmentService.londOne(id);
    }
    @GetMapping
    @PreAuthorize(value = "department/findAll",name = "查询所有部门")
    public List<Department> finAll(){
        return departmentService.loadAll();
    }
    @GetMapping("/tree")
    public List<Department> tree(){
        return departmentService.tree();
    }

    @DeleteMapping("/{id}")
    @PreAuthorize(value = "department/removeById",name = "删除部门根据ID")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            departmentService.removeById(id);
            return AjaxResult.me().setMessage("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败:"+e.getMessage());
        }
    }
    @PutMapping    //{success:true,messasge:编辑成功,data:}
    @PreAuthorize(value = "department/addAndEdit",name = "添加修改部门")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if(department.getId()==null){
                departmentService.save(department);
            }else{
                departmentService.editById(department);
            }
            return AjaxResult.me().setMessage("编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("编辑失败:"+e.getMessage());
        }
    }
    @PatchMapping
    @PreAuthorize(value = "department/patchDel",name = "批量删除")
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            departmentService.patchDel(ids);
            return AjaxResult.me().setMessage("批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量删除失败");
        }
    }
    @PostMapping
    @PreAuthorize(value = "department/queryPage",name = "部门分页查询")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery){
        PageList<Department> page = departmentService.queryData(departmentQuery);
        return page;
    }
}
